﻿/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Uptime
* Version: 0.2
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["uptime"] = new Object();
myPlugins["uptime"]["name"] = getLocalizedString2("mod_sysm_plugin_up_name");
myPlugins["uptime"]["onlyonce"] = true;
myPlugins["uptime"]["newObject"] = function (NewSettings) { return new plugin_uptime_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_uptime_sensor(NewSettings) {
	// Required variables
	this.plugin = "uptime";
	this.settings = NewSettings;
	this.edit = plugin_uptime_edit;
	this.ReDraw = plugin_uptime_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;
	//this.frame.style.backgroundColor = "red";


	// Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.record) this.settings.record = 0;
	if(!this.settings.shorttext) this.settings.shorttext = 0;
	if(!this.settings.showrecord) this.settings.showrecord = 0;


	// The Sensore
	this.uptime = new class_TextLine(this.frame,0,getLocalizedString2("mod_sysm_plugin_up_label1"));
	this.record = new class_TextLine(this.frame,mod_sysmonitor_size,getLocalizedString2("mod_sysm_plugin_up_label2"));
	this.uptime.label.color = this.settings.color;
	this.uptime.data.color = this.settings.color;
	this.record.label.color = this.settings.color;
	this.record.data.color = this.settings.color;

	// Update Timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = 60;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_uptime_refresh.call(obj); };
	plugin_uptime_refresh.call(obj);


	// Calculating sensore height (required!)
	if(this.settings.showrecord==1) {
		this.record.data.visible = true;
		this.record.label.visible = true;
		this.frame.height = this.record.data.vOffset + (this.record.data.size * 1/3);
	} else {
		this.record.data.visible = false;
		this.record.label.visible = false;
		this.frame.height = this.uptime.data.vOffset + (this.uptime.data.size * 1/3);
	}
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_uptime_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_up_pref_short");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.shorttext;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_up_pref_record");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.showrecord;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_up_pref_reset");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = 0;

		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.uptime.label.color = this.settings.color;
			this.uptime.data.color = this.settings.color;
			this.record.label.color = this.settings.color;
			this.record.data.color = this.settings.color;
			this.settings.shorttext = Drive_Edit_DialogResults[1];
			if(Drive_Edit_DialogResults[3]==1) this.settings.record = 0;
			if(this.settings.showrecord != Drive_Edit_DialogResults[2]) {
				this.settings.showrecord = Drive_Edit_DialogResults[2];

				if(this.settings.showrecord==1) {
					this.record.data.visible = true;
					this.record.label.visible = true;
					this.frame.height = this.record.data.vOffset + (this.record.data.size * 1/3);
				} else {
					this.record.data.visible = false;
					this.record.label.visible = false;
					this.frame.height = this.uptime.data.vOffset + (this.uptime.data.size * 1/3);
				}
				mod_sysmonitor_ReDraw();
			}
			plugin_uptime_refresh.call(obj);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_uptime_refresh() {
		var tmp = 0;
		if(system.platform=="windows") {
			// Old Style: tmp = wmi.Get('Win32_PerfFormattedData_PerfOS_System=@').Properties_.Item("SystemUpTime").Value;
			// Better Version to also get it on 2k System:
			var tmp_sut = wmi.Get('Win32_PerfRawData_PerfOS_System=@').Properties_.Item("SystemUpTime").value;
			var tmp_fo = wmi.Get('Win32_PerfRawData_PerfOS_System=@').Properties_.Item("Frequency_Object").value;
			var tmp_to = wmi.Get('Win32_PerfRawData_PerfOS_System=@').Properties_.Item("Timestamp_Object").value;
			tmp = (tmp_to - tmp_sut) / tmp_fo;
		} else if(system.platform=="macintosh") {
			tmp = runCommand("uptime");
			tmp = tmp.substring(0,tmp.indexOf("load")-2);
			tmp = tmp.substring(tmp.indexOf("up")+3,tmp.lastIndexOf(","));
			tmp = getMinutes(tmp.split(",")) * 60;
		}

		if( tmp > parseInt(this.settings.record) ) this.settings.record = tmp;
		this.uptime.data.data = Date2String(tmp,this.settings.shorttext);
		this.record.data.data = Date2String(this.settings.record,this.settings.shorttext);
		return true;
	}

	function Date2String(timeinseconds,shorttext) {
		var timeDifference = timeinseconds;
		dRemain	= Math.floor(timeDifference / (60 * 60 * 24));
		timeDifference -= dRemain * (60 * 60 * 24);
		hRemain = Math.floor(timeDifference / (60 * 60)); 
		timeDifference -= hRemain * (60 * 60);
		mRemain = Math.floor(timeDifference / (60)); 
		//timeDifference -= mRemain * (60);
		//sRemain = Math.round(timeDifference);
	
		var tmp;
		if(shorttext==1) {
			//tmp = dRemain + "d " + hRemain + "h " + mRemain + "m " + sRemain + "s";
			tmp = dRemain + getLocalizedString2("mod_sysm_plugin_up_dayS") + " " + hRemain + getLocalizedString2("mod_sysm_plugin_up_hourS") + " " + mRemain + getLocalizedString2("mod_sysm_plugin_up_minuteS");
		} else {
			if(dRemain!=1) tmp = dRemain + " " + getLocalizedString2("mod_sysm_plugin_up_dayM") + ", ";
			else tmp = dRemain + " " + getLocalizedString2("mod_sysm_plugin_up_day") + ", ";
			if(hRemain!=1) tmp += hRemain + " " + getLocalizedString2("mod_sysm_plugin_up_hourM") + ", ";
			else tmp += hRemain + " " + getLocalizedString2("mod_sysm_plugin_up_hour") + ", ";
			if(mRemain!=1) tmp += mRemain + " " + getLocalizedString2("mod_sysm_plugin_up_minuteM");
			else tmp += mRemain + " " + getLocalizedString2("mod_sysm_plugin_up_minute");
		}
		return tmp;
	}

	function getMinutes(string) {
		var currentUp = 0;
		for(i=string.length-1;i>=0;i--) {
			// minutes and hours
			if(i==string.length-1) {
				var timePart = string[i].split(":");
				// we have both hours and minutes
				if(timePart.length > 1) {
					currentUp = Number(timePart[1]);
					currentUp += Number(timePart[0]*60);
				}
				// either less than one hour or a whole amount of hours
				else {
					timePart = string[i].split(" ");
					// remove extra array element
					if(timePart[0]=="") {
						timePart = timePart.slice(1);
					}
					if(timePart[1]=="hrs" || timePart[1]=="hours" || timePart[1]=="hour") {
						currentUp = Number(timePart[0]*60);
					} else if(timePart[1]=="min" || timePart[1]=="mins") {
						currentUp = Number(timePart[0]);
					}
				}
			}
			// we have days
			else if(i==string.length-2) {
				timePart = string[i].split(" ");
				currentUp += Number(timePart[0]*24*60);
			}
			// reaching weeks
			else if(i==string.length-3) {
				timePart = string[i].split(" ");
				currentUp += Number(timePart[0]*7*24*60);
			}
		}
		return currentUp;
	}

	function plugin_uptime_ReDraw() {
		this.frame.width = mod_sysmonitor_width;

		this.uptime.size = mod_sysmonitor_size;
		this.record.size = mod_sysmonitor_size;
		this.record.vOffset = mod_sysmonitor_size;

		if(this.settings.showrecord==1) {
			this.frame.height = this.record.data.vOffset + (this.record.data.size * 1/3);
		} else {
			this.frame.height = this.uptime.data.vOffset + (this.record.data.size * 1/3);
		}

		return true;
	}

}

// --------------------------------------------------------------------------------
// The END